package com.cloudlibrary.service.impl;


import com.cloudlibrary.domain.Book;
import com.cloudlibrary.entity.PageResult;
import com.cloudlibrary.service.BookService;
import com.cloudlibrary.mapper.BookMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @author 33582
* @description 针对表【book】的数据库操作Service实现
* @createDate 2024-12-12 15:29:10
*/
@Service
public class BookServiceImpl implements BookService{
    @Autowired
    private BookMapper bookMapper;
    @Override
    public PageResult selectNewBooks(Integer pageNum, Integer PageSize) {
        PageHelper.startPage(pageNum, PageSize);
        Page<Book> page = bookMapper.selectNewBooks();
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Book findById(String id) {
        return bookMapper.findById(id);
    }

    @Override
    public Integer borrowBook(Book book) {
        Book b = this.findById(String.valueOf(book.getId()));
        DateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd");
        book.setBorrowTime(dataFormat.format(new Date()));
        book.setStatus("1");
        book.setPrice(b.getPrice());
        book.setUploadTime(b.getUploadTime());

        return bookMapper.editBook(book);


    }
}




